//设置cookie
var setCookie = function (name, value, day) {
    //当设置的时间等于0时，不设置expires属性，cookie在浏览器关闭后删除
    var expires = day * 24 * 60 * 60 * 1000;
    var exp = new Date();
    exp.setTime(exp.getTime() + expires);
    document.cookie = name + "=" + value + ";expires=" + exp.toUTCString();
    //document.cookie = value;
};


//获取cookie
function getCookie(name) {
    // 拆分 cookie 字符串
    var cookieArr = document.cookie.split(";");

    // 循环遍历数组元素
    for (var i = 0; i < cookieArr.length; i++) {
        var cookiePair = cookieArr[i].split("=");

        /* 删除 cookie 名称开头的空白并将其与给定字符串进行比较 */
        if (name == cookiePair[0].trim()) {
            // 解码cookie值并返回
            return decodeURIComponent(cookiePair[1]);
        }
    }
    // 如果未找到，则返回null
    return null;
}
// data=["华为","oppo","apple"];
// console.log(data);
var a, b;
function qbtnfunction() {
    console.log("qbtn");
    a = document.getElementById("min").value;
    b = document.getElementById("max").value;
    console.log(a, b);
    $("#display_area").html("");
    pagefunc();
}

//聲明cookie
function cookiefunc(params){
    setCookie("productId", params, 7)
}


//向后端发起展示商品请求
//var func_page = function pagefunc(){
function pagefunc() {
    var pro_type=$.cookie("pro_type");
    $.ajax({
        url: "http://8.130.103.251:8080/good/getpage",//选择了这些类型
        type: "get",
        data: {
            tag: pro_type,
            goodBrand: null,
            //subtwo:itemlist[i].sub[j].name
            min: a,
            max: b,
            order: priceorder,
            pagenum: pagenumber,
            pagesize: 15,
            buyTimeOrder:buyTimeOrder,
        },
        contentType: 'application/json;charset=utf-8',
        success: function (result) {
            var productId=[];
            //返回选定类型的商品
            for (var x = 0; x < result.records.length; x++) {
                //$("#one").removeClass("hidden");
                //console.log(result.records[x].goodid);
                
                productId.push(result.records[x].goodid);
                //console.log(productId[y]);
             
                

                var displayarea = document.getElementById('display_area');
                //创建单个商品展示区域
                var topDiv = document.createElement('div');//创建div块
                $(topDiv).addClass("recommend-content-item");//为div块增添样式
                topDiv.id = "top_div"//为div块设置id
                var top_div = document.getElementById('top_div');
                displayarea.appendChild(topDiv);//将div块加到展示区域
                //创建跳转商品详情页
                var a = document.createElement("a");
                $(a).addClass("item-link");
                a.id = "top_a";
                var top_a = document.getElementById('top_a');
                a.target="_blank";
                a.href = "productDetails.html";//跳转商品详情页
                //点击a标签后生成cookie
                
                
                topDiv.appendChild(a);
                //console.log(top_div);
                //创建aDiv展示商品图片
                var aDiv = document.createElement('div');
                $(aDiv).addClass("img-wrapper");
                aDiv.id = "a_Div";
                var a_Div = document.getElementById('a_Div');
                a.appendChild(aDiv);
                //var myDiv = document.getElementById('myDiv');//获得dom对象 
                //创建一个img
                var Img = document.createElement("img");
                Img.src = result.records[x].goodImg1;
                aDiv.appendChild(Img);
                a.addEventListener("click", cookiefunc.bind(a, productId[x]));
                //创建bDiv展示商品名称
                var bDiv = document.createElement('div');
                $(bDiv).addClass("info-wrapper");
                bDiv.id = "b_div";
                var b_div = document.getElementById('b_div');
                a.appendChild(bDiv);
                //创建一个div放商品名
                var bDivOne = document.createElement('div');
                $(bDivOne).addClass("title");
                bDivOne.id = "b_div_one";
                bDivOne.innerText = result.records[x].goodName;
                bDiv.appendChild(bDivOne);

                //创建cDiv展示商品价格
                var cDiv = document.createElement('div');
                $(cDiv).addClass("price-wrapper");
                cDiv.id = "c_div";
                var c_div = document.getElementById('c_div');
                a.appendChild(cDiv);
                //创建span放商品价格
                var cSpanOne = document.createElement('span');
                $(cSpanOne).addClass("title");
                cSpanOne.id = "c_span_one";
                cSpanOne.innerText = "¥" + result.records[x].goodPrice;
                cDiv.appendChild(cSpanOne);
            }
        }
    })
}



let pagenumber = 1;
//处理分页

//点击第一页
$("#li_one").click(function () {
    $("#li_two").removeClass("am-active");
    $("#li_three").removeClass("am-active");
    $("#li_four").removeClass("am-active");
    $("#li_one").addClass("am-active");
    $("#li_five").removeClass("am-active");
    pagenumber = 1;
    $("#display_area").html("");
    pagefunc();
})

//点击第二页
$("#li_two").click(function () {
    $("#li_two").addClass("am-active");
    $("#li_three").removeClass("am-active");
    $("#li_four").removeClass("am-active");
    $("#li_one").removeClass("am-active");
    $("#li_five").removeClass("am-active");
    pagenumber = 2;
    $("#display_area").html("");
    pagefunc();
})

//点击第三页
$("#li_three").click(function () {
    $("#li_two").removeClass("am-active");
    $("#li_three").addClass("am-active");
    $("#li_four").removeClass("am-active");
    $("#li_one").removeClass("am-active");
    $("#li_five").removeClass("am-active");
    pagenumber = 3;
    $("#display_area").html("");
    pagefunc();
})

//点击第四页
$("#li_four").click(function () {
    $("#li_two").removeClass("am-active");
    $("#li_three").removeClass("am-active");
    $("#li_four").addClass("am-active");
    $("#li_one").removeClass("am-active");
    $("#li_five").removeClass("am-active");
    pagenumber = 4;
    $("#display_area").html("");
    pagefunc();
})

//点击第五页
$("#li_five").click(function () {
    $("#li_two").removeClass("am-active");
    $("#li_three").removeClass("am-active");
    $("#li_four").removeClass("am-active");
    $("#li_one").removeClass("am-active");
    $("#li_five").addClass("am-active");
    pagenumber = 5;
    $("#display_area").html("");
    pagefunc();
})


//加载页面时向后端请求数据
window.onload = function () {

    var pro_type=$.cookie("pro_type");
    console.log(pro_type);
    $.ajax({
        url: "http://8.130.103.251:8080/good/getSubTitleByType",//调用选择商品类型
        type: "get",

        
        data: {
            type: pro_type,
        },
        //data:obj,
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            $("#i_first").html(data);
        }
    })

    $.ajax({
        url: "http://8.130.103.251:8080/good/getBrandByType",//调用选择商品类型
        type: "get",

        data: {
            type: pro_type,
        },
        //data:obj,
        contentType: 'application/json;charset=utf-8',
        success: function (data) {

            var td = [];

            var k = 0;
            var row = document.getElementById("tr_one");
            //console.log($("#tr_one").children(1));
            for (var j = 0, i = 0; j < data.length; j++, i++) {
                td[i] = document.createElement('td');
                //td.innerHTML = itemlist[i].sub[j].name;
                td[i].style.textAlign
                td[i].style.borderRightStyle
                $(td[i]).addClass("td_mid");
                $(td[i]).addClass("p_two");
                td[i].innerHTML = data[j];//.value
                // row[k].appendChild(td);
                //td[i].className("td_mid");
                //console.log(td[i]);
                var td_ = [];
                td_[k] = td[i].innerHTML;

                //$(td[i]).on("click",function(td_){})

                //td[i].onclick=func(td_);
                td[i].addEventListener("click", func.bind(td[i], td_[k]));
                row.appendChild(td[i]);
                // td[i].id=k;
                // var td = document.getElementById(k);
                //tbody.appendChild(row);
                k++;
            };

        }
    })
    //初始页面显示全部商品
    pagefunc();



}

//点击便签显示商品
function func(params) {
    var pro_type=$.cookie("pro_type");
    $("#display_area").html("");
    $.ajax({
        url: "http://8.130.103.251:8080/good/getpage",//选择了这些类型
        type: "get",
        data: {
            tag: pro_type,
            goodBrand: params,
            //subtwo:itemlist[i].sub[j].name
            min: a,
            max: b,
            order: true,
            pagenum: pagenumber,
            pagesize: 15,
        },
        contentType: 'application/json;charset=utf-8',
        success: function (result) {
          
            var productId=[];

            //返回选定类型的商品
            for (var x = 0; x < result.records.length; x++) {
                //$ ("#one").removeClass("hidden");

                productId.push(result.records[x].goodid);


                var displayarea = document.getElementById('display_area');
                //创建单个商品展示区域
                var topDiv = document.createElement('div');//创建div块
                $(topDiv).addClass("recommend-content-item");//为div块增添样式
                topDiv.id = "top_div"//为div块设置id
                var top_div = document.getElementById('top_div');
                displayarea.appendChild(topDiv);//将div块加到展示区域
                //创建跳转商品详情页
                var a = document.createElement("a");
                $(a).addClass("item-link");
                a.id = "top_a";
                var top_a = document.getElementById('top_a');
                a.target="_blank";
                a.href = "productDetails.html";//跳转商品详情页
                a.addEventListener("click", cookiefunc.bind(a, productId[x]));

                topDiv.appendChild(a);
                //console.log(top_div);
                //创建aDiv展示商品图片
                var aDiv = document.createElement('div');
                $(aDiv).addClass("img-wrapper");
                aDiv.id = "a_Div";
                var a_Div = document.getElementById('a_Div');
                a.appendChild(aDiv);
                //var myDiv = document.getElementById('myDiv');//获得dom对象 
                //创建一个img
                var Img = document.createElement("img");
                Img.src = result.records[x].goodImg1;
                aDiv.appendChild(Img);

                //创建bDiv展示商品名称
                var bDiv = document.createElement('div');
                $(bDiv).addClass("info-wrapper");
                bDiv.id = "b_div";
                var b_div = document.getElementById('b_div');
                a.appendChild(bDiv);
                //创建一个div放商品名
                var bDivOne = document.createElement('div');
                $(bDivOne).addClass("title");
                bDivOne.id = "b_div_one";
                bDivOne.innerText = result.records[x].goodName;
                bDiv.appendChild(bDivOne);

                //创建cDiv展示商品价格
                var cDiv = document.createElement('div');
                $(cDiv).addClass("price-wrapper");
                cDiv.id = "c_div";
                var c_div = document.getElementById('c_div');
                a.appendChild(cDiv);
                //创建span放商品价格
                var cSpanOne = document.createElement('span');
                $(cSpanOne).addClass("title");
                cSpanOne.id = "c_span_one";
                cSpanOne.innerText = "¥" + result.records[x].goodPrice;
                cDiv.appendChild(cSpanOne);
            }
        }
    })
}

$("#price_order").click(function () {
    $("#dropdown").dropdown("toggle");
   
})

var priceorder;
//点击由低到高
$("#low_high").click(function () {
    $("#high_low").removeClass("am-active");
    $("#low_high").addClass("am-active");
    //$("#dropdown").dropdown("close");
    //$("#price_order").innerHTML="由低到高";
    priceorder=true;
    $("#display_area").html("");
    pagefunc();
})
//点击由高到低
$("#high_low").click(function () {
    $("#low_high").removeClass("am-active");
    $("#high_low").addClass("am-active");
    //$("#dropdown").dropdown("close");
    //$("#price_order").innerHTML="由低到高";
    priceorder=false;
    $("#display_area").html("");
    pagefunc();
})

//点击综合排序
$("#zonghe").click(function () {
    $("#xiaoliang").removeClass("am-active");
    $("#zonghe").addClass("am-active");
    $("#display_area").html("");
    pagefunc();
})

//点击销量排序
var buyTimeOrder;
$("#xiaoliang").click(function () {
    $("#xiaoliang").addClass("am-active");
    $("#zonghe").removeClass("am-active");
    $("#display_area").html("");
    buyTimeOrder=1;
    pagefunc();
})


$(document).ready(function (){
    //读取登录状态
    var if_login=$.cookie("status");
    //读取用户名
    var username=$.cookie("username");
    
    if(if_login==1){
        $("#login_region").text(username);
        $("#dropdown-login-item").css("display","none");
        $("#dropdown-exit-item").css("display","block");
        
    }else if(if_login==0){
        $("#login_region").text("登录/注册");
        $("#dropdown-login-item").css("display","block");
        $("#dropdown-exit-item").css("display","none");
    }
});

//退出按钮的点击事件
$("#dropdown-exit-item").on("click",function () { 
    $.cookie('username', null);
    $.cookie('status', '0', { expires: 7 });
    location.reload ();
})



//导航栏顶部跳转数码专区
$("#digital-area").click(function(){
    $.cookie('pro_type',"数码配件",{ expires: 7 });
    window.open("tags.html");
});




